package com.cskaoyan.market.service.wx;

import com.cskaoyan.market.db.domain.MarketCategory;
import com.cskaoyan.market.db.domain.MarketCategoryExample;
import com.cskaoyan.market.db.mapper.MarketCategoryMapper;
import com.cskaoyan.market.vo.CatalogCurrentVo;
import com.cskaoyan.market.vo.CatalogIndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxCatalogServiceImpl implements WxCatalogService {


    @Autowired
    MarketCategoryMapper categoryMapper;

    @Override
    public CatalogCurrentVo current(Integer id) {
        //要返回的数据
        CatalogCurrentVo catalogCurrentVo = new CatalogCurrentVo();
        MarketCategory marketCategory = categoryMapper.selectByPrimaryKey(id);
        //如果此id的删除标记为true，代表该条目已被删除，则直接返回默认值
        if(marketCategory.getDeleted()){
            return catalogCurrentVo;
        }
        catalogCurrentVo.setCurrentCategory(marketCategory);
        //接下来查找二级目录数据
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(id);
        criteria.andDeletedEqualTo(false);
        //通过二级目录的pid等于一级目录的id，通过这个条件筛选二级目录
        List<MarketCategory> l2Categories = categoryMapper.selectByExample(categoryExample);
        catalogCurrentVo.setCurrentSubCategory(l2Categories);
        return catalogCurrentVo;
    }

    @Override
    public CatalogIndexVo index() {
        CatalogIndexVo catalogIndexVo = new CatalogIndexVo();
        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        criteria.andDeletedEqualTo(false);
        List<MarketCategory> categories = categoryMapper.selectByExample(categoryExample);
        //在此层暂只设置一级目录的列表，current的内容可直接套用另一个接口，在controller层实现
        catalogIndexVo.setCategoryList(categories);
        //初始默认显示第一条一级目录的内容
        Integer id=categories.get(0).getId();
        CatalogCurrentVo current = current(id);
        catalogIndexVo.setCurrentCategory(current.getCurrentCategory());
        catalogIndexVo.setCurrentSubCategory(current.getCurrentSubCategory());
        return catalogIndexVo;
    }

}

